<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\analysis\data\material\stat;

use app\model\mongo\MongoMaterialAreaMonthStatModel;
use think\Model;

trait AreaMonthStatTrait
{
    /**
     * @var int 汇总月份
     */
    protected int $statMonth = 0;

    /**
     * @var int 汇总区域
     */
    protected int $areaId = 0;

    /**
     * @var int 任务类型
     */
    protected int $taskType = 0;

    /**
     * 设置汇总.
     */
    public function setStat(): bool
    {
        if (! $this->setAreaMonthStatByType()) {
            return false;
        }

        return $this->setAreaTotalMonthStat();
    }

    /**
     * 设置素材区域月汇总.
     */
    private function setAreaMonthStatByType(): bool
    {
        $model = $this->getAreaMonthStatModel($this->taskType);
        if (! $model instanceof MongoMaterialAreaMonthStatModel) {
            return self::setError('未知素材区域月汇总模型');
        }
        if (! $model->getKey()) {
            $data = [
                'stat_month' => $this->statMonth,
                'area_id' => $this->areaId,
                'type' => $this->taskType,
                'qq' => ['cost' => 0],
                'oe' => ['cost' => 0],
                'ks' => ['cost' => 0],
            ];
        } else {
            $data = [];
        }
        $data = $this->getAreaMonthStatField($data);
        $model->setAttrs($data);
        if (! $model->save()) {
            return self::setError('保存素材区域月汇总失败');
        }
        return true;
    }

    /**
     * 设置图片素材区域月汇总.
     */
    private function setAreaTotalMonthStat(): bool
    {
        $model = $this->getAreaMonthStatModel();
        if (! $model instanceof MongoMaterialAreaMonthStatModel) {
            return self::setError('未知图片素材区域月汇总模型');
        }
        if (! $model->getKey()) {
            $data = [
                'stat_month' => $this->statMonth,
                'area_id' => $this->areaId,
                'type' => 0,
                'qq' => ['cost' => 0],
                'oe' => ['cost' => 0],
                'ks' => ['cost' => 0],
            ];
        } else {
            $data = [];
        }
        $data = $this->getAreaTotalMonthStatField($data);
        $model->setAttrs($data);
        if (! $model->save()) {
            return self::setError('保存图片素材区域月汇总失败');
        }
        return true;
    }

    /**
     * 获取区域汇总模型.
     * @param int $type 0：全部 1：视频 2：图片
     * @return array|mixed|Model|MongoMaterialAreaMonthStatModel
     */
    private function getAreaMonthStatModel(int $type = 0)
    {
        return (new MongoMaterialAreaMonthStatModel())
            ->where([
                ['stat_month', '=', $this->statMonth],
                ['area_id', '=', $this->areaId],
                ['type', '=', $type],
            ])
            ->findOrEmpty();
    }

    /**
     * 获取区域月汇总数据.
     * @param array $data 更新数据
     */
    private function getAreaTotalMonthStatField(array $data = []): array
    {
        // 消耗查询
        $map = [
            ['stat_month', '=', $this->statMonth],
            ['type', '>', 0],
        ];

        $data['qq']['cost'] = (new MongoMaterialAreaMonthStatModel())->where($map)->sum('qq.cost');
        $data['oe']['cost'] = (new MongoMaterialAreaMonthStatModel())->where($map)->sum('oe.cost');
        $data['ks']['cost'] = (new MongoMaterialAreaMonthStatModel())->where($map)->sum('ks.cost');

        return $data;
    }
}
